package com.ordering.action.admin.product;

import java.io.IOException;
import java.io.PrintWriter;

import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;

import com.ordering.action.BaseAction;
import com.ordering.constant.ResultResponse;
import com.ordering.entity.OrderItem;
import com.ordering.entity.Product;
import com.ordering.service.product.ProductService;
import com.ordering.service.product.impl.ProductServiceImpl;
import com.ordering.util.DateAndTime;

public class ProductManagerAction  extends BaseAction
{
    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    private Product product;
    
    private long productId;
    
    private String modifyMessage = null;
    
    ProductService productService = new ProductServiceImpl();
    
    public String add(){
        try
        {
            product.setCreateTime(DateAndTime.getCurrentTime());
            product.setLastModifyTime(DateAndTime.getCurrentTime());
            product.setRealPrice(product.getPrice());
            productService.add(product);
        }
        catch (Exception e)
        {
            return FAIL;
        }
        
        return SUCCESS;
    }
    
    public String toModify(){
        try
        {
        	product =  productService.getById(productId);
        }
        catch (Exception e)
        {
            return FAIL;
        }
        
        return "toModify";
    }
    
    public String modifyExc(){
    	
        try
        {
        	ResultResponse re = new ResultResponse();
        	product.setCreateTime(product.getCreateTime());
            product.setLastModifyTime(DateAndTime.getCurrentTime());
            product.setRealPrice(product.getPrice());
            re = productService.update(product);
            
            if(re.getCode() != ResultResponse.CODE_SUCCESS){
        		throw new Exception(re.getMessage());
        	}
            //outPutMessage("<script type='text/javascript'>alert('"+product.getName()+" 修改成功');</script>");
            modifyMessage = product.getName()+" 修改成功";
            return "modifyExc";
        }
        catch (Exception e)
        {
        	e.printStackTrace();
        	return FAIL;
        }
        
    }
    
    
    public void toDown(){
    	JSONObject jsonObject = new JSONObject();
        try
        {
        	
        	ResultResponse re = new ResultResponse();
        	jsonObject.put("status",true);
        	re = productService.updateStatus(productId, 3);
        	if(re.getCode() != ResultResponse.CODE_SUCCESS){
        		jsonObject.put("status",false);
        		jsonObject.put("message",re.getMessage());
        		throw new Exception(re.getMessage());
        	}
        	
        	outPutMessage(jsonObject.toString());
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        
    }
    
    
    private void outPutMessage(String jsonStr) throws IOException{
	    String contentType = "text/html;charset=utf-8";
		//指定输出内容类型和编码  
        ServletActionContext.getResponse().setContentType(contentType);   
        //获取输出流，然后使用  
        PrintWriter out = ServletActionContext.getResponse().getWriter();
        out.print(jsonStr);
        out.flush();  
        out.close(); 
	}
    
    
    public Product getProduct()
    {
        return product;
    }

    public void setProduct(Product product)
    {
        this.product = product;
    }

    public void setProductService(ProductService productService)
    {
        this.productService = productService;
    }

	public long getProductId() {
		return productId;
	}

	public void setProductId(long productId) {
		this.productId = productId;
	}

	public String getModifyMessage() {
		return modifyMessage;
	}

	public void setModifyMessage(String modifyMessage) {
		this.modifyMessage = modifyMessage;
	}
    
    
}
